#include "stm32f1xx_it.h"

void NMI_Handler(void) {
}


void HardFault_Handler(void) {
    while (1) {
    }
}


void MemManage_Handler(void) {
    while (1) {
    }
}


void BusFault_Handler(void) {
    while (1) {
    }
}


void UsageFault_Handler(void) {
    while (1) {
    }
}


void SVC_Handler(void) {
}


void DebugMon_Handler(void) {
}


void PendSV_Handler(void) {
}


void SysTick_Handler(void) {
}


void DMA1_Channel4_IRQHandler(void) {
    if(LL_DMA_IsActiveFlag_TC4(DMA1)) {
        LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_4);
        LL_USART_DisableDMAReq_TX(USART1);
        LL_DMA_ClearFlag_TC4(DMA1);
    }
}

void DMA1_Channel7_IRQHandler(void){
    if(LL_DMA_IsActiveFlag_TC7(DMA1)) {
        LL_DMA_DisableChannel(DMA1, LL_DMA_CHANNEL_7);
        LL_USART_DisableDMAReq_TX(USART2);
        LL_DMA_ClearFlag_TC7(DMA1);
    }
}
/* 定时通过USART1发送命令中断*/
void TIM5_IRQHandler(void) {
    LL_TIM_ClearFlag_UPDATE(TIM5);
    SendCmdViaDMA((uint32_t)TxBuf, 5);
}


void USART1_IRQHandler(void) {
    // 发射板接收到数据,马上开始扫描
    if(LL_USART_IsActiveFlag_RXNE(USART1)) {
        *RxBuf = *(RxBuf + 1);
        (*(uint32_t*)(&RxBuf[1])) >>= 8;
        *(RxBuf + 4) = LL_USART_ReceiveData8(USART1);

        if(*(uint16_t*)(RxBuf) == CMD_HEAD) {
            RxMsgBackup = *RxDataAddr;
            NewMsgFlag = 1;
        }
    }

    // 发送完一个字节,或DMA全部发送完
    if(LL_USART_IsActiveFlag_TC(USART1)) {
        LL_USART_ClearFlag_TC(USART1);
        LL_USART_ClearFlag_RXNE(USART1);
        LL_USART_EnableIT_RXNE(USART1);
        IO_RS485_RT = 0;
    }
}


void USART2_IRQHandler(void){
    if(LL_USART_IsActiveFlag_TC(USART2)) {
        LL_USART_ClearFlag_TC(USART2);
    }
}
void DMA2_Channel3_IRQHandler(void) {
    if(LL_DMA_IsActiveFlag_TC3(DMA2)) {
        LL_DMA_ClearFlag_TC3(DMA2);
    }

    // 循环模式代表在播放正弦波
    if(LL_DMA_GetMode(DMA2, LL_DMA_CHANNEL_3) == LL_DMA_MODE_CIRCULAR) {
        if(--remain_T == 0) {
            // 关闭定时器
            LL_TIM_DisableCounter(TIM6);
            // 关闭DMA请求
            LL_DAC_DisableDMAReq(DAC1, LL_DAC_CHANNEL_1);
            // 关闭DMA
            LL_DMA_DisableChannel(DMA2, LL_DMA_CHANNEL_3);
            // 关闭DAC
            LL_DAC_Disable(DAC1, LL_DAC_CHANNEL_1);
        }
    } else {
        // 关闭定时器
        LL_TIM_DisableCounter(TIM6);
        // 关闭DMA请求
        LL_DAC_DisableDMAReq(DAC1, LL_DAC_CHANNEL_1);
        // 关闭DMA
        LL_DMA_DisableChannel(DMA2, LL_DMA_CHANNEL_3);
        // 关闭DAC
        LL_DAC_Disable(DAC1, LL_DAC_CHANNEL_1);

        // 设置地址
        if(stopAddr > startAddr) {
            LL_DMA_SetMemoryAddress(DMA2, LL_DMA_CHANNEL_3, startAddr);

            // 设置长度
            if(stopAddr - startAddr > 0xFFFF) {
                LL_DMA_SetDataLength(DMA2, LL_DMA_CHANNEL_3, 0xFFFF);
                startAddr = stopAddr - 0xFFFF;
            } else {
                LL_DMA_SetDataLength(DMA2, LL_DMA_CHANNEL_3, stopAddr - startAddr);
                startAddr = stopAddr;
            }

            // 打开DMA请求允许
            LL_DAC_EnableDMAReq(DAC1, LL_DAC_CHANNEL_1);
            // 使能通道
            LL_DMA_EnableChannel(DMA2, LL_DMA_CHANNEL_3);
            // 开启DAC
            LL_DAC_Enable(DAC1, LL_DAC_CHANNEL_1);
            // 让TIM6开始触发
            LL_TIM_EnableCounter(TIM6);
        }
    }
}